const blogDao = require("../dao/blogDao")
const tagDao = require("../dao/tagDao")
const commentDao = require("../dao/commentDao")
const tagBlogMapping = require('../dao/tagBlogMappingDao')
const timeUtil = require("../util/timeUtil")
const respUtil = require("../util/respUtil")
const url = require('url');
const captcha = require("svg-captcha")
const path = new Map();


function insertComment(req, res) {
    var params = url.parse(req.url, true).query;
    
    commentDao.insertComment(params.bid, params.parent, params.parent_name, params.user_name, params.content, params.email, timeUtil.getNow(), timeUtil.getNow(), function(result) {
        res.writeHead(200);
        res.write(respUtil.writeResult("success", "添加成功", null))
        res.end();
        
    })
   
}

path.set("/insertComment", insertComment);


function queryRandomCode(req, res) {
    const img = captcha.create({fontSize: 50, width: 100, height: 34});
    res.writeHead(200);
    res.write(respUtil.writeResult("success", "评论成功", img))
    res.end();
}

path.set("/queryRandomCode", queryRandomCode)

function queryCommentsByBlogId(req, res) {
    var params = url.parse(req.url, true).query;
    commentDao.queryCommentsByBlogId(parseInt(params.bid), function (result) {
        res.writeHead(200);
        res.write(respUtil.writeResult("success", "评论成功", result))
        res.end();
    })
}

path.set("/queryCommentsByBlogId", queryCommentsByBlogId)

function queryBlogCommentsCount(req, res) {
    var params = url.parse(req.url, true).query;
    commentDao.queryBlogCommentsCount(parseInt(params.bid), function (result) {
        res.writeHead(200);
        res.write(respUtil.writeResult("success", "评论成功", result))
        res.end();
    })
    
}
path.set("/queryBlogCommentsCount", queryBlogCommentsCount)

function queryNewComment(req, res) {
    commentDao.queryNewComment(5, function (result) {
        res.writeHead(200);
        res.write(respUtil.writeResult("success", "评论成功", result))
        res.end();
    })
    
}
path.set("/queryNewComment", queryNewComment)


module.exports.path = path;